import axios from 'axios'
import  type {InternalAxiosRequestConfig} from 'axios'
import { ElMessage } from 'element-plus'
let service= axios.create({
    baseURL:"/VueHandler",
    timeout:5000
})




service.interceptors.request.use((config:InternalAxiosRequestConfig<any>)=>{
   if(config.method=="get"){ //name=1&age=2
    config.headers["Content-Type"]="application/x-www-form-urlencoded"
   } 
//    if(config.method=="post"){ //{name:1,age:2}
//     config.headers["Content-Type"]="application/json"
//    }
  let token=localStorage.getItem("token")
  if(token){
    config.headers.Authorization='Bearer '+token
  }
  return config
})

service.interceptors.response.use((response)=>{
    if(response.data.code=="401"){
        ElMessage.error("请先登录")
    }
    if(response.status==400){
        ElMessage.error("请求参数错误")
    }
     if(response.status==502){
        ElMessage.error("服务器暂时维护中")
    }
     if(response.status==503){
        ElMessage.error("服务器宕机")
     }
    return response.data
})

export default service